#ifndef CONNECTION_H
#define CONNECTION_H
#include <string>
#include <stdint.h>
struct bufferevent;
class Service;
class ConnectionHandler;
struct event;

class Connection
{
public:
	~Connection();
	Connection(bufferevent* bev, Service* service);
	Connection(Service* service);
	void connect(std::string ip, int port);
	void setHandler(ConnectionHandler* handler);
	void* getBuffer(int len);
	int getBufferLen();
	void removeBuffer(int len);
	//bufferevent* getBev();
	void close();
	int getId();
	void sendData(char* data, int len);
	void* getUserData();
	void setUserData(void*);
private:
	void realClose();
private:
	static void onBevWrite(bufferevent* bev, void* args);
	static void onBevRead(bufferevent* bev, void* args);
	static void onBevEvent(bufferevent* bev, short ev, void* args);
	static void onTmrClose(int fd, short ev, void* args);
private:
	std::string m_ip;
	int m_port;
	ConnectionHandler* m_handler;
	event* m_tmrClose;
	bufferevent* m_bev;
	Service* m_service;
	void* m_userData;
	int m_fd;
	int m_connId;
};

#endif
